﻿using DataAccess;
using DataAccess.DAL;
using DataAccess.DTO;
using DataAccess.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Xml;


namespace Web.Controllers
{
    public class BaseController : Controller
    {


        static AgencyUsersDAL userDAL = new AgencyUsersDAL();
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {

            if (UserSession == null || string.IsNullOrEmpty(UserSession.user_name))
            {
                filterContext.Result = new RedirectResult("/login.html");
            }
            else
            {
                ViewBag.NickName = UserSession.nick_name;
               
            }
            //判断是否有访问权限
            //{
            //    string actionName = filterContext.ActionDescriptor.ActionName;
            //    string controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
            //    bool boo = false;
            //    if ((actionName.ToLower() != "index" && controllerName.ToLower() != "home") && boo)
            //    {
            //        filterContext.Result = new RedirectResult("~/authorize.html");
                 
            //    }

            //}
            base.OnActionExecuting(filterContext);



            //HttpContextBase ctx = filterContext.HttpContext;
       
            //{
            //    if (ctx.Session == null)
            //    {
            //        ClearLogin();
            //        ctx.Response.Redirect("~/login.html");
            //    }
            //    else
            //    {
            //        HttpCookie Authorization = ctx.Request.Cookies["Authorization"];
            //        if (Authorization == null)
            //        {
            //            ClearLogin();
            //            ctx.Response.Redirect("~/login.html");
            //        }
            //        else if (string.IsNullOrEmpty(Authorization.Value))
            //        {
            //            ClearLogin();
            //            ctx.Response.Redirect("~/login.html");
            //        }
            //        else
            //        {
            //            UserSession session = XYD.Common.JsonHelper.DeserializeJson<UserSession>(XYD.Common.EncodeSecurity.DecryptString(XYD.Common.Utils.UrlDecode(Authorization.Value)));
            //            ViewBag.NickName = session.nick_name;
                         
            //            if (session == null)
            //            {
            //                ClearLogin();
            //                ctx.Response.Redirect("~/login.html");
            //            }
            //            else if (session.time_stamp < XYD.Common.TimeStampHelper.GenerateTimeStamp(DateTime.Now) - 24 * 3600)
            //            {
            //                ClearLogin();
            //                ctx.Response.Redirect("~/login.html");
            //            }
            //        }
            //    }
            //}
          
          
          
           
            base.OnActionExecuting(filterContext);
        }

        public void ClearLogin()
        {
            HttpCookie cookie = Request.Cookies["Authorization"];
            if (cookie != null)
            {
                cookie.Expires = DateTime.Now.AddDays(-2);
                Response.Cookies.Set(cookie);
            }
        }

        
        private UserSession _userSession;
        public UserSession UserSession
        {

            get
            {
                if (_userSession != null)
                {
                    return _userSession;
                }
                try
                {
                    if (System.Web.HttpContext.Current.Request.Cookies[CookieHelp.UserCookieKey] != null && !string.IsNullOrEmpty(System.Web.HttpContext.Current.Request.Cookies[CookieHelp.UserCookieKey].Value))
                    {
                        string EncryptData = System.Web.HttpContext.Current.Request.Cookies[CookieHelp.UserCookieKey].Value;
                        UserSession curAdmin = XYD.Common.JsonHelper.DeserializeJson<UserSession>(XYD.Common.EncodeSecurity.DecryptString(EncryptData));
                        ViewBag.Admin = curAdmin;
                        _userSession = curAdmin;
                        return curAdmin;
                    }
                }
                catch (Exception)
                {
                    return null;
                }
                return null;
            }
            set
            {
                _userSession = value;
            }
        }


        private SysConfig sysConfig;

        public SysConfig SysConfig
        {
            get
            {

                if (sysConfig == null)
                {
                    var key = RedisKey.SysConfig_promoter_id(UserSession.user_id.ToString());

                    if (!RedisHelper.Exist(key))
                    {

                        sysConfig = new DataAccess.DAL.SysConfigDAL().Get_PromoterId(UserSession.user_id);
                        if (sysConfig == null)
                            sysConfig = new DataAccess.Models.SysConfig();
                        RedisHelper.Single_Set_Itme<SysConfig>(key, sysConfig);
                    }
                    return RedisHelper.Single_Get_Itme<SysConfig>(key);

                }
                return sysConfig;

            }

        }
       

    }


    public class Validate
    {
        //校验用户名密码
        public static bool ValidateAccount(string username, string password)
        {
            if (username == "admin" && password == "123123")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool ValidateTicket(string encryptTicket)
        {
            //解密Ticket
            var strTicket = XYD.Common.EncodeSecurity.DecryptString(encryptTicket);

            //从Ticket里面获取用户名和密码
            string[] up = strTicket.Split('&');
            if (up.Length < 2)
            {
                return false;
            }

            return ValidateAccount(up[0], up[1]);
        }
    }

}